package com.simple.action.fee;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.BaseExtendedProperties;
import com.simple.po.FeeDetail;
import com.simple.po.FeeReport;
import com.simple.po.FeeRetention;
import com.simple.po.FeeWrittenOff;
import com.simple.service.fee.FeeService;
import com.simple.service.fee.FeeWrittenOffReportService;
import com.simple.service.fee.FeeWrittenOffService;
import com.xunj.action.common.AbstractAction;
import com.xunj.core.ParaUnit;
import com.xunj.core.SessionBean;
import com.xunj.service.system.CodeService;
import com.xunj.util.Common;
import com.xunj.util.Util;

/**
 * 费用核销
 * @author 王瑾
 *
 */
public class FeeWrittenOffAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -365972686059686745L;
	private FeeDetail feeDetail; //费用登记
	private FeeReport feeReport; //费用上报
	private FeeWrittenOff feeWrittenOff; //费用核销


	


	/**
	 * 查询已上报或已核销的上报费用信息
	 * @return
	 * @throws Exception
	 */
	public String listFeeWrittenOff() throws Exception
	{
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		
		if(POST())
		{
			if(!feeReport.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId", feeReport.getAreaId(), ParaUnit.EQ));
			if(!feeReport.getAreaDeptId().equals(""))
				paralist.add(new ParaUnit("areaDeptId", feeReport.getAreaDeptId(), ParaUnit.EQ));
			
			if(!feeReport.getFeeReportYear().equals(""))
				paralist.add(new ParaUnit("feeReportYear", feeReport.getFeeReportYear(), ParaUnit.EQ));
			if(!feeReport.getFeeReportMonth().equals(""))
				paralist.add(new ParaUnit("feeReportMonth", feeReport.getFeeReportMonth(), ParaUnit.EQ));
			if(!feeReport.getReportState().equals(""))
				paralist.add(new ParaUnit("reportState", feeReport.getReportState(), ParaUnit.EQ));
			else
			{
				String[] state={"已上报","已核销","核销完毕","已报销"};
				paralist.add(new ParaUnit("reportState", state, ParaUnit.IN));
			}
			if(!feeReport.getFeeReportCode().equals(""))
				paralist.add(new ParaUnit("feeReportCode", feeReport.getFeeReportCode(), ParaUnit.LIKE));
			
		}
		else
		{
			String[] state={"已上报","已核销","核销完毕","已报销"};
			paralist.add(new ParaUnit("reportState", state, ParaUnit.IN));
		}
		List list = dao.criteriaByPage(FeeReport.class, paralist);

		request.setAttribute("datalist", list);
		return toJsp("/jsp/fee/fee_written_off/lst_fee_written_off.jsp");
	}
	/**
	 * 费用审核
	 * @return
	 * @throws Exception
	 */
	public String showExamineFeeWrittenOff() throws Exception
	{
		String feeReportId = request.getParameter("feeReportId");
		
		feeReport = (FeeReport)dao.findOne(FeeReport.class, feeReportId);

		FeeService fs = new FeeService(dao);
		FeeWrittenOffService fwos = new FeeWrittenOffService(dao);
		//费用核销明细
		
		List writtenOffList = fs.queryFeeJoin("", " and feeDetail.feeReportId='"+feeReportId+"'", "order by feeDetail.feeTarget",false);
		
		Map dataMap = fwos.createWrittenOffTolData(writtenOffList, true, true);
		
		feeReport.setBillAmountTol((Double) dataMap.get("billAmountTol"));
		feeReport.setBillCountTol((Integer) dataMap.get("billCountTol"));
		feeReport.setCertificateAmountTol((Double) dataMap.get("certificateAmountTol"));
		feeReport.setCertificateCountTol((Integer) dataMap.get("certificateCountTol"));
		dao.update(feeReport);
		
		
		request.setAttribute("tdTitle1", dataMap.get("tdTitle1"));
		request.setAttribute("tdTitle2", dataMap.get("tdTitle2"));
		request.setAttribute("tdTitle3", dataMap.get("tdTitle3"));
		request.setAttribute("tdTitle4", dataMap.get("tdTitle4"));
		request.setAttribute("listHtml", dataMap.get("listHtml"));
		request.setAttribute("writtenOffList", writtenOffList);
		String edit = request.getParameter("edit");
		request.setAttribute("edit", edit);
		
		return toJsp("/jsp/fee/fee_written_off/show_tol_fee_written_off.jsp");
	}
	/**
	 * 核销费用
	 * @return
	 * @throws Exception
	 */
	public String updateDetailFeeWrittenOff() throws Exception
	{
		if(POST())
		{
			//当前汇总的上一次本次留审与调整前期留审数额
			String billAmountLeaveReview_o = request.getParameter("billAmountLeaveReview_o");
			String billCountLeaveReview_o = request.getParameter("billCountLeaveReview_o");
			String certificateAmountLeaveReview_o = request.getParameter("certificateAmountLeaveReview_o");
			String certificateCountLeaveReview_o = request.getParameter("certificateCountLeaveReview_o");
			String billAmountLeaveReviewEarly_o = request.getParameter("billAmountLeaveReviewEarly_o");
			String billCountLeaveReviewEarly_o = request.getParameter("billCountLeaveReviewEarly_o");
			String certificateAmountLeaveReviewEarly_o = request.getParameter("certificateAmountLeaveReviewEarly_o");
			String certificateCountLeaveReviewEarly_o = request.getParameter("certificateCountLeaveReviewEarly_o");
			feeDetail = (FeeDetail) dao.findOne(FeeDetail.class, feeWrittenOff.getFeeId());
			feeDetail.setState("核销");
			FeeRetention feeRetention = (FeeRetention) dao.findOne(FeeRetention.class, feeDetail.getTargetId());
			
			if(feeRetention==null)
			{
				feeRetention = new FeeRetention();
				feeRetention.setTargetId(feeDetail.getTargetId());
				feeRetention.setTargetName(feeDetail.getTargetName());
				feeRetention.setBillAmountLeaveReview(0d);
				feeRetention.setBillCountLeaveReview(0);
				feeRetention.setCertificateAmountLeaveReview(0d);
				feeRetention.setCertificateCountLeaveReview(0);
				dao.save(feeRetention);
			}
			
			
			if("".equals(billAmountLeaveReview_o))
				billAmountLeaveReview_o = "0";
			if("".equals(billCountLeaveReview_o))
				billCountLeaveReview_o = "0";
			if("".equals(certificateAmountLeaveReview_o))
				certificateAmountLeaveReview_o = "0";
			if("".equals(certificateCountLeaveReview_o))
				certificateCountLeaveReview_o = "0";
			if("".equals(billAmountLeaveReviewEarly_o))
				billAmountLeaveReviewEarly_o = "0";
			if("".equals(billCountLeaveReviewEarly_o))
				billCountLeaveReviewEarly_o = "0";
			if("".equals(certificateAmountLeaveReviewEarly_o))
				certificateAmountLeaveReviewEarly_o = "0";
			if("".equals(certificateCountLeaveReviewEarly_o))
				certificateCountLeaveReviewEarly_o = "0";
			//留审额+前期留审调整额-本次留神调整额-上次留神额+本次留审额
			feeRetention.setBillAmountLeaveReview(Common.sum(feeRetention.getBillAmountLeaveReview(),
					Double.parseDouble(billAmountLeaveReviewEarly_o),
					(feeWrittenOff.getBillAmountLeaveReviewEarly()==null?0:feeWrittenOff.getBillAmountLeaveReviewEarly())*-1,
					Double.parseDouble(billAmountLeaveReview_o)*-1,
					feeWrittenOff.getBillAmountLeaveReview()));
			feeRetention.setBillCountLeaveReview(Common.sum(feeRetention.getBillCountLeaveReview(),
					Integer.parseInt(billCountLeaveReviewEarly_o),
					(feeWrittenOff.getBillCountLeaveReviewEarly()==null?0:feeWrittenOff.getBillCountLeaveReviewEarly())*-1,
					Integer.parseInt(billCountLeaveReview_o)*-1,
					feeWrittenOff.getBillCountLeaveReview()));
			feeRetention.setCertificateAmountLeaveReview(Common.sum(feeRetention.getCertificateAmountLeaveReview(),
					Double.parseDouble(certificateAmountLeaveReviewEarly_o),
					(feeWrittenOff.getCertificateAmountLeaveReviewEarly()==null?0:feeWrittenOff.getCertificateAmountLeaveReviewEarly())*-1,
					Double.parseDouble(certificateAmountLeaveReview_o)*-1,
					feeWrittenOff.getCertificateAmountLeaveReview()));
			feeRetention.setCertificateCountLeaveReview(Common.sum(feeRetention.getCertificateCountLeaveReview(),
					Integer.parseInt(certificateCountLeaveReviewEarly_o),
					(feeWrittenOff.getCertificateCountLeaveReviewEarly()==null?0:feeWrittenOff.getCertificateCountLeaveReviewEarly())*-1,
					Integer.parseInt(certificateCountLeaveReview_o)*-1,
					feeWrittenOff.getCertificateCountLeaveReview()));
			
			dao.update(feeWrittenOff);
			dao.update(feeRetention);
			feeReport = (FeeReport)dao.findOne(FeeReport.class, feeWrittenOff.getFeeReportId());
			feeReport.setReportState("已核销");
			String nextFeeId = request.getParameter("nextFeeId");
			if(nextFeeId.equals(""))
				nextFeeId = feeDetail.getFeeId();
			this.Alert_GoUrl("保存成功", "updateDetailFeeWrittenOff.do?dataIsUpdated=true&" +
					"nextFeeId="+nextFeeId+
					"&feeTypeId="+feeDetail.getFeeTypeId()+
					"&feeReportId="+feeDetail.getFeeReportId()+
					"&targetId="+feeDetail.getTargetId());
			return toMessage();
		}
		else
		{
			String nextFeeId = request.getParameter("nextFeeId");
			
			String targetId = request.getParameter("targetId");
			String feeTypeId = request.getParameter("feeTypeId");
			String feeReportId = request.getParameter("feeReportId");
			ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
			paralist.add(new ParaUnit("targetId", targetId,ParaUnit.EQ));
			paralist.add(new ParaUnit("feeReportId", feeReportId,ParaUnit.EQ));
			paralist.add(new ParaUnit("feeTypeId", feeTypeId,ParaUnit.EQ));
			paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
			List feeDetailList = dao.criteriaQuery(FeeDetail.class, paralist);
			request.setAttribute("feeDetailList", feeDetailList);
			if(nextFeeId!=null&&!"".equals(nextFeeId))
			{
				for(int i=0;i<feeDetailList.size();i++)
				{
					feeDetail = (FeeDetail) feeDetailList.get(i);
					if(feeDetail.getFeeId().equals(nextFeeId))
						break;
				}
				feeWrittenOff = (FeeWrittenOff) dao.findOne(FeeWrittenOff.class, nextFeeId);
			}
			else
			{
				feeDetail = (FeeDetail) feeDetailList.get(0);
				feeWrittenOff = (FeeWrittenOff) dao.findOne(FeeWrittenOff.class, feeDetail.getFeeId());
			}
			
			FeeRetention feeRetention = (FeeRetention) dao.findOne(FeeRetention.class, feeDetail.getTargetId());
			request.setAttribute("targetId", targetId);
			request.setAttribute("feeTypeId", feeTypeId);
			request.setAttribute("feeReportId", feeReportId);
			request.setAttribute("feeRetention", feeRetention);
			String edit = request.getParameter("edit");
			if("false".equals(edit))
				return toJsp("/jsp/fee/fee_written_off/show_fee_written_off.jsp");
			else
				return toJsp("/jsp/fee/fee_written_off/update_fee_written_off.jsp");
		}
	}
	/**
	 * 统计生效，费用核销完毕
	 * @return
	 * @throws Exception
	 */
	public String updateFeeWrittenOff() throws Exception
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		SessionBean sessionBean  = SessionBean.getSessionBean(request);
		
		FeeReport fr = (FeeReport)dao.findOne(FeeReport.class, feeReport.getFeeReportId());
		fr.setReportState("核销完毕");
		fr.setWrittenOffDate(new Date());
		fr.setWrittenOffUserId(sessionBean.getUserId());
		fr.setWrittenOffUserName(sessionBean.getUserName());
		fr.setWrittenOffReportRemark(feeReport.getWrittenOffReportRemark());
		fr.setOtherFeeRemark(feeReport.getOtherFeeRemark());
		//处理凭证号数据
		if(fr.getCertificateCountTol()!=null&&fr.getCertificateCountTol()>0)
		{
			BaseExtendedProperties properties = (BaseExtendedProperties) dao.findOne(BaseExtendedProperties.class, "certificateNo");
			int certificateNoStart = 1;
			int certificateNoEnd = fr.getCertificateCountTol();
			if(properties==null)
			{
				properties = new BaseExtendedProperties();
				properties.setExtendedPropertiesId("certificateNo");
				properties.setBelongModel("费用管理");
				properties.setOptionalValue(""+fr.getCertificateCountTol());
				properties.setPropertiesName("凭证号");
				properties.setPropertiesNotes(Common.getDateTimeFormat(new Date(), "yyyyMM"));
				dao.save(properties);
				
			}else
			{
				//当前月份
				if(properties.getPropertiesNotes().equals(Common.getDateTimeFormat(new Date(), "yyyyMM")))
				{
					certificateNoStart = Integer.parseInt(properties.getOptionalValue())+1;
					certificateNoEnd = Integer.parseInt(properties.getOptionalValue())+fr.getCertificateCountTol();
					properties.setOptionalValue(certificateNoEnd+"");
				}
				else
				{
					properties.setPropertiesNotes(Common.getDateTimeFormat(new Date(), "yyyyMM"));
					properties.setOptionalValue(""+fr.getCertificateCountTol());
				}
				dao.update(properties);
			}
		
			fr.setCertificateNoStart(certificateNoStart);
			fr.setCertificateNoEnd(certificateNoEnd);
		}
		dao.update(fr);
		//将费用核销明细字段置为核销完毕
		String hql = "update FeeDetail f set f.state = '核销完毕' where f.feeReportId='"+fr.getFeeReportId()+"'";
		dao.bulkUpdate(hql);
		this.Alert_GoUrl("保存成功", "listFeeWrittenOff.do");
		return toMessage();

	}
	/**
	 * 撤销上报
	 * @return
	 * @throws Exception
	 */
	public String updateBackFeeWrittenOff() throws Exception
	{
		String feeReportId = request.getParameter("feeReportId");
		
		feeReport = (FeeReport)dao.findOne(FeeReport.class, feeReportId);
		feeReport.setReportState("未上报");
		feeReport.setBillAmountTol(null);
		feeReport.setBillCountTol(null);
		feeReport.setCertificateAmountTol(null);
		feeReport.setCertificateCountTol(null);
		feeReport.setCertificateNoStart(null);
		feeReport.setCertificateNoEnd(null);
		
		
		//汇总明细
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
		paralist.add(new ParaUnit("feeReportId", feeReportId, ParaUnit.EQ));
		List writtenOffList = dao.criteriaQuery(FeeWrittenOff.class, paralist);
		List feeDetailList = dao.criteriaQuery(FeeDetail.class, paralist);
		String[] targetIds = new String[writtenOffList.size()];
		for(int i=0;i<feeDetailList.size();i++)
		{
			FeeDetail fd = (FeeDetail) feeDetailList.get(i);
			targetIds[i] = fd.getTargetId();
		}
		Util u = new Util();
		if(targetIds.length>0)
		{
			//作为清空费用核销信息的模板对象
			FeeWrittenOff emptyFwo = new FeeWrittenOff();
			paralist.clear();
			paralist.add(new ParaUnit("targetId", targetIds, ParaUnit.IN));
			List feeRetentionList = dao.criteriaQuery(FeeRetention.class, paralist);
			//费用留审汇总中的数额=费用留审汇总中的数额-本次留审数额+前期留审调整
			for(int j=0;j<feeRetentionList.size();j++)
			{
				FeeRetention feeRetention = (FeeRetention) feeRetentionList.get(j);
				
				for(int i=0;i<writtenOffList.size();i++)
				{
					FeeWrittenOff fwo = (FeeWrittenOff) writtenOffList.get(i);
					FeeDetail fd = null;
					for(int k=0;k<feeDetailList.size();k++)
					{
						if(fd.getFeeId().equals(fwo.getFeeId()))
						{
							fd = (FeeDetail) feeDetailList.get(k);
							break;
						}
					}
					
					if(feeRetention.getTargetId().equals(fd.getTargetId()))
					{
						emptyFwo.setFeeId(fd.getFeeId());
						feeRetention.setBillAmountLeaveReview(Common.sum(feeRetention.getBillAmountLeaveReview(),
								(fwo.getBillAmountLeaveReview()==null?0:fwo.getBillAmountLeaveReview())*-1,	
								(fwo.getBillAmountLeaveReviewEarly()==null?0:fwo.getBillAmountLeaveReviewEarly())		
								));
						feeRetention.setBillCountLeaveReview(Common.sum(feeRetention.getBillCountLeaveReview(),
								(fwo.getBillCountLeaveReview()==null?0:fwo.getBillCountLeaveReview())*-1,	
								(fwo.getBillCountLeaveReviewEarly()==null?0:fwo.getBillCountLeaveReviewEarly())		
						));
						feeRetention.setCertificateAmountLeaveReview(Common.sum(feeRetention.getCertificateAmountLeaveReview(),
								(fwo.getCertificateAmountLeaveReview()==null?0:fwo.getCertificateAmountLeaveReview())*-1,	
								(fwo.getCertificateAmountLeaveReviewEarly()==null?0:fwo.getCertificateAmountLeaveReviewEarly())		
						));
						feeRetention.setCertificateCountLeaveReview(Common.sum(feeRetention.getCertificateCountLeaveReview(),
								(fwo.getCertificateCountLeaveReview()==null?0:fwo.getCertificateCountLeaveReview())*-1,	
								(fwo.getCertificateCountLeaveReviewEarly()==null?0:fwo.getCertificateCountLeaveReviewEarly())		
						));
						//清空核销表数值
						u.copySameProperties(emptyFwo, fwo);
						fwo.setBillAmountWrittenOff(fd.getBillAmount());
						fwo.setBillCountWrittenOff(fd.getBillCount());
						fwo.setCertificateAmountWrittenOff(fd.getCertificateAmount());
						fwo.setCertificateCountWrittenOff(fd.getCertificateCount());
					}
				}
			}
		}
		
		dao.bulkUpdate("delete from FeeWrittenOff where feeReportId='"+feeReportId+"'");
		dao.bulkUpdate("update FeeDetail set state='在用' where feeReportId='"+feeReportId+"' and (state='上报' or state='核销完毕')");
		this.Alert_Close("撤销上报成功", "ok");
		return toMessage();
		
	}
	/**
	 * 费用核销统计查询
	 * @return
	 * @throws Exception
	 */
	public String queryFeeWrittenOff() throws Exception
	{
		String areaId = request.getParameter("areaId");
		String areaDeptId = request.getParameter("areaDeptId");
		String feeReportYearFrom = request.getParameter("feeReportYearFrom");
		String feeReportYearTo = request.getParameter("feeReportYearTo");
		String feeReportMonthFrom = request.getParameter("feeReportMonthFrom");
		String feeReportMonthTo = request.getParameter("feeReportMonthTo");
		String[] reportState = request.getParameterValues("reportState");
		String feeTypeId = request.getParameter("feeTypeId");
		String feeReportCode = request.getParameter("feeReportCode");
		String targetRadio = request.getParameter("targetRadio");
		String showData = request.getParameter("showData");
		boolean showSbje = false;
		if(showData!=null&&showData.equals("申报"))
			showSbje = true;
		
		List codeList = (List) this.getServletContext().getAttribute("CodeDict");
		CodeService codeService = new CodeService();
		String pageTitle = "";
		String secondTitle = "";
		
		String feeDetailCondition = " and feeDetail.feeReportId in ( select fr.feeReportId ";
		String feeReportHql = " from FeeReport fr where 1=1 ";

		if(areaId!=null&&!"".equals(areaId))
		{
			feeReportHql += " and fr.areaId ='"+areaId+"'";
			secondTitle = "地区："+codeService.getCodeNameByCodeId(codeList, areaId);
		}
		if(areaDeptId!=null&&!"".equals(areaDeptId))
		{
			feeReportHql += " and fr.areaDeptId ='"+areaDeptId+"'";
			pageTitle += codeService.getCodeNameByCodeId(codeList, areaDeptId);
		}
		if(feeReportYearFrom!=null&&!"".equals(feeReportYearFrom))
		{
			feeReportHql += " and fr.feeReportYear >="+feeReportYearFrom;
			pageTitle += feeReportYearFrom+"年";
		}
		if(feeReportMonthFrom!=null&&!"".equals(feeReportMonthFrom))
		{
			feeReportHql += " and fr.feeReportMonth >="+feeReportMonthFrom;
			pageTitle += feeReportMonthFrom+"月";
		}
		if(feeReportYearTo!=null&&!"".equals(feeReportYearTo))
		{
			feeReportHql += " and fr.feeReportYear <="+feeReportYearTo;
			pageTitle += "至"+feeReportYearTo+"年";
		}
		if(feeReportMonthTo!=null&&!"".equals(feeReportMonthTo))
		{
			feeReportHql += " and fr.feeReportMonth <="+feeReportMonthTo;
			pageTitle += feeReportMonthTo+"月";
		}
		if(reportState.length>0)
		{
			feeReportHql += " and fr.reportState in (";
			for(int i=0;i<reportState.length;i++)
			{
				feeReportHql += "'"+reportState[i]+"',";
			}
			feeReportHql = feeReportHql.substring(0,feeReportHql.length()-1);
			feeReportHql += ") ";
		}
		if(feeReportCode!=null&&!"".equals(feeReportCode))
			feeReportHql += " and fr.feeReportCode ='"+feeReportCode+"'";
		
		feeDetailCondition+=feeReportHql+")";
		
		if(feeTypeId!=null&&!"".equals(feeTypeId))
		{
			feeDetailCondition += " and feeDetail.feeTypeId like '"+feeTypeId+"%'";
			pageTitle += codeService.getCodeNameByCodeId(codeList, feeTypeId);
		}
		FeeService fs = new FeeService(dao);
		FeeWrittenOffService fwos = new FeeWrittenOffService(dao);
		//费用核销明细
		
		List writtenOffList = fs.queryFeeJoin("", feeDetailCondition, "order by feeDetail.feeTarget",false);
		
		if(writtenOffList.size()==0)
		{
			this.Alert_Close("所选条件中无费用信息", "ok");
			return toMessage();
		}
		Map dataMap = fwos.createWrittenOffTolData(writtenOffList, false,showSbje);
		
		
		
		request.setAttribute("tdTitle1", dataMap.get("tdTitle1"));
		request.setAttribute("tdTitle2", dataMap.get("tdTitle2"));
		request.setAttribute("tdTitle3", dataMap.get("tdTitle3"));
		request.setAttribute("tdTitle4", dataMap.get("tdTitle4"));
		request.setAttribute("listHtml", dataMap.get("listHtml"));
		request.setAttribute("titleColspan", dataMap.get("titleColspan"));
		request.setAttribute("pageTitle", pageTitle);
		request.setAttribute("writtenOffList", writtenOffList);
		
		//如果是查询出一个费用上报信息
		List feeReportList = dao.findAll("select fr "+feeReportHql);
		if(feeReportList.size()==1)
		{
			feeReport = (FeeReport) feeReportList.get(0);
			secondTitle = "地区："+feeReport.getAreaName();
		}
		else
		{
			Double billAmountTol = 0d;
			Integer billCountTol = 0;
			Double certificateAmountTol = 0d;
			Integer certificateCountTol = 0;
			for(int i=0;i<feeReportList.size();i++)
			{
				FeeReport fr = (FeeReport) feeReportList.get(i);
				billAmountTol = Common.sum(billAmountTol,fr.getBillAmountTol());
				billCountTol = Common.sum(billCountTol,fr.getBillCountTol());
				certificateAmountTol = Common.sum(certificateAmountTol,fr.getCertificateAmountTol());
				certificateCountTol = Common.sum(certificateCountTol,fr.getCertificateCountTol());
			}
			request.setAttribute("billAmountTol", billAmountTol);
			request.setAttribute("billCountTol", billCountTol);
			request.setAttribute("certificateAmountTol", certificateAmountTol);
			request.setAttribute("certificateCountTol", certificateCountTol);
		}
		request.setAttribute("secondTitle", secondTitle);
		if(targetRadio.equals("excel"))
			return toJsp("/jsp/fee/fee_written_off/show_tol_fee_written_off_excel.jsp");
		else
			return toJsp("/jsp/fee/fee_written_off/show_tol_fee_written_off_page.jsp");
	}
	/**
	 * 费用核销统计查询
	 * @return
	 * @throws Exception
	 */
	public String queryAreaFeeWrittenOff() throws Exception
	{
		String areaDeptId = request.getParameter("areaDeptId");
		String feeTarget = request.getParameter("feeTarget");
		String targetId = request.getParameter("targetId");
		String feeReportYearFrom = request.getParameter("feeReportYearFrom");
		String feeReportYearTo = request.getParameter("feeReportYearTo");
		String feeReportMonthFrom = request.getParameter("feeReportMonthFrom");
		String feeReportMonthTo = request.getParameter("feeReportMonthTo");
		String[] reportState = request.getParameterValues("reportState");
		String feeTypeId = request.getParameter("feeTypeId");
		String targetRadio = request.getParameter("targetRadio");
		String showData = request.getParameter("showData");
		boolean showSbje = false;
		if(showData!=null&&showData.equals("申报"))
			showSbje = true;
		
		List codeList = (List) this.getServletContext().getAttribute("CodeDict");
		CodeService codeService = new CodeService();
		String pageTitle = "";
		String secondTitle = "";
		
		String appendCondition = " ";

		if(areaDeptId!=null&&!"".equals(areaDeptId))
		{
			appendCondition += " and feeReport.areaDeptId ='"+areaDeptId+"'";
			secondTitle += "[事业部:"+codeService.getCodeNameByCodeId(codeList, areaDeptId)+"]";
		}
		secondTitle += "[日期:";
		if(feeReportYearFrom!=null&&!"".equals(feeReportYearFrom))
		{
			appendCondition += " and feeReport.feeReportYear >="+feeReportYearFrom;
			secondTitle += feeReportYearFrom+"年";
		}
		if(feeReportMonthFrom!=null&&!"".equals(feeReportMonthFrom))
		{
			appendCondition += " and feeReport.feeReportMonth >="+feeReportMonthFrom;
			secondTitle += feeReportMonthFrom+"月";
		}
		if(feeReportYearTo!=null&&!"".equals(feeReportYearTo))
		{
			appendCondition += " and feeReport.feeReportYear <="+feeReportYearTo;
			secondTitle += "至"+feeReportYearTo+"年";
		}
		if(feeReportMonthTo!=null&&!"".equals(feeReportMonthTo))
		{
			appendCondition += " and feeReport.feeReportMonth <="+feeReportMonthTo;
			secondTitle += feeReportMonthTo+"月";
		}
		secondTitle+="]";
		if(reportState.length>0)
		{
			appendCondition += " and feeReport.reportState in (";
			for(int i=0;i<reportState.length;i++)
			{
				appendCondition += "'"+reportState[i]+"',";
			}
			appendCondition = appendCondition.substring(0,appendCondition.length()-1);
			appendCondition += ") ";
		}
		if(feeTypeId!=null&&!"".equals(feeTypeId))
		{
			appendCondition += " and feeDetail.feeTypeId like '"+feeTypeId+"%'";
			pageTitle += codeService.getCodeNameByCodeId(codeList, feeTypeId);
		}
		if(feeTarget!=null&&!"".equals(feeTarget))
		{
			appendCondition += " and feeDetail.feeTarget = '"+feeTarget+"'";
			secondTitle += "["+feeTarget+":";
			if(targetId.equals(""))
				secondTitle+="全部]";
			else
				secondTitle+=request.getParameter("targetName")+"]";
		}
		if(targetId!=null&&!"".equals(targetId))
		{
			appendCondition += " and feeDetail.targetId = '"+targetId+"'";
		}
		
		//费用核销明细
		
		FeeWrittenOffReportService fs = new FeeWrittenOffReportService(dao);
		List writtenOffList = fs.queryFeeTotal("", appendCondition, "order by feeReport.areaId");
		
		if(writtenOffList.size()==0)
		{
			this.Alert_Close("所选条件中无费用信息", "ok");
			return toMessage();
		}

		Map dataMap = fs.createWrittenOffTolData(writtenOffList, false,showSbje);
		
		
		
		request.setAttribute("tdTitle1", dataMap.get("tdTitle1"));
		request.setAttribute("tdTitle2", dataMap.get("tdTitle2"));
		request.setAttribute("tdTitle3", dataMap.get("tdTitle3"));
		request.setAttribute("tdTitle4", dataMap.get("tdTitle4"));
		request.setAttribute("listHtml", dataMap.get("listHtml"));
		request.setAttribute("titleColspan", dataMap.get("titleColspan"));
		pageTitle+="地区费用明细汇总表";
		request.setAttribute("pageTitle", pageTitle);
		request.setAttribute("secondTitle", secondTitle);
		request.setAttribute("writtenOffList", writtenOffList);
		

		if(targetRadio.equals("excel"))
			return toJsp("/jsp/fee/fee_written_off/show_tol_fee_written_off_excel.jsp");
		else
			return toJsp("/jsp/fee/fee_written_off/show_tol_fee_written_off_page.jsp");
	}
	public FeeDetail getFeeDetail() {
		return feeDetail;
	}
	public void setFeeDetail(FeeDetail feeDetail) {
		this.feeDetail = feeDetail;
	}

	public FeeReport getFeeReport() {
		return feeReport;
	}

	public void setFeeReport(FeeReport feeReport) {
		this.feeReport = feeReport;
	}
	public FeeWrittenOff getFeeWrittenOff() {
		return feeWrittenOff;
	}
	public void setFeeWrittenOff(FeeWrittenOff feeWrittenOff) {
		this.feeWrittenOff = feeWrittenOff;
	}


}